﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ServidorGestionClientes;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Configuration;
using System.Collections;
using System.Runtime.Serialization.Formatters;

namespace ServicioGestionClientes
{
    public class ServicioGestionClientes : IServicioGestionClientes
    {
        public bool ValidarLogIn(string nombre, string contrasenia)
        {
            try
            {
                string dir = (ConfigurationManager.AppSettings["dirRemotingWs"]);

                if (RemotingConfiguration.IsRemotelyActivatedClientType(typeof(RemotingWS)) == null)
                {
                    //TcpChannel chan = new TcpChannel();
                    //ChannelServices.RegisterChannel(chan, false);
                    IDictionary props = new Hashtable();
                    props["port"] = 0;
                    BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
                    serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
                    IChannel chan = new TcpChannel(props, null, serverProvider);
                    ChannelServices.RegisterChannel(chan, false);

                    //Metodo para poder usar el objeto remoto
                    RemotingConfiguration.RegisterActivatedClientType(typeof(RemotingWS), dir);


                }

                RemotingWS servidorGestion = (RemotingWS)Activator.GetObject(typeof(RemotingWS), dir);

                bool ok = servidorGestion.Loguear(nombre, contrasenia);

                return ok;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public Tuple<bool, bool, string, string, string> InfoCliente(string nombre)
        {
            try
            {
                string dir = (ConfigurationManager.AppSettings["dirRemotingWs"]);

                if (RemotingConfiguration.IsRemotelyActivatedClientType(typeof(RemotingWS)) == null)
                {
                    TcpChannel chan = new TcpChannel();
                    ChannelServices.RegisterChannel(chan, false);

                    //Metodo para poder usar el objeto remoto
                    RemotingConfiguration.RegisterActivatedClientType(typeof(RemotingWS), dir);
                }

                RemotingWS servidorGestion = (RemotingWS)Activator.GetObject(typeof(RemotingWS), dir);


                Tuple<bool, bool, string, string, string> tup = new Tuple<bool, bool, string, string, string>(false, false, "", "", "");
                List<Cliente> lisC = servidorGestion.ListaDeClientes();
                foreach (Cliente c in lisC)
                {
                    if (c.Nombre.Equals(nombre))
                    {
                        tup = new Tuple<bool, bool, string, string, string>(c.EsPosPago, c.EstaEnFuncionamiento, c.Saldo.ToString(), c.InfoConsumos(), c.TotalConsumido());
                    }
                }
                return tup;
            }
            catch (Exception)
            {
                return new Tuple<bool, bool, string, string, string>(false, false, "", "", "");
            }
        }
    }
}
